package com.example.mobileeducationtv;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ReplySMSActivity extends Activity {
	
	static String tag = "Variable Passed";
	String SENT = "SMS_SENT";
	String DELIVERED = "SMS_DELIVERED";
	PendingIntent sentPI, deliveredPI;
	BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reply_sms);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		    String msg = extras.getString("message");
		    String num = extras.getString("number");
		    TextView tv = (TextView)findViewById(R.id.tv);
		    TextView tvnum = (TextView)findViewById(R.id.tvnum);
		    tvnum.setText(num);
		    tv.setText(msg);
		    Log.i(tag, "Msg is received");
		}
		
		sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
		deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		smsSentReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context arg0, Intent arg1) {
				// TODO Auto-generated method stub
				switch (getResultCode()) {
					case Activity.RESULT_OK:
						Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
						Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_NO_SERVICE:
						Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_NULL_PDU:
						Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_RADIO_OFF:
						Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
						break;
				}
			}
		};
		
		smsDeliveredReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				switch (getResultCode()) {
					case Activity.RESULT_OK:
						Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
						break;
					case Activity.RESULT_CANCELED:
						Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
						break;
				}
			}
			
		};
		
		registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));
		registerReceiver(smsSentReceiver, new IntentFilter(SENT));
	}
	
	@Override
	public void onPause() {
		super.onPause();
		unregisterReceiver(smsSentReceiver);
		unregisterReceiver(smsDeliveredReceiver);
	}
	
	public void returnToActivity(View view){
	    finish();
	}
	
	public void onClick(View v) {
		sendSMS();
	}
	
	public void sendSMS() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		    String num = extras.getString("number");
		    EditText message;
		    message = (EditText)findViewById(R.id.contactMessage);
		    String msg = message.getText().toString();
		    SmsManager sendSMS = SmsManager.getDefault();
			sendSMS.sendTextMessage(num, null, msg, sentPI, deliveredPI);
		}
	}
}
